# coding: utf-8

# -------------------------------------------------------------------------------
# Name:         send_mail_task.py
# Description:  
# Author:       XiangjunZhao
# EMAIL:        2419352654@qq.com
# Date:         2019/11/13 9:19
# -------------------------------------------------------------------------------
import json
from celery import shared_task
from django.core.mail import EmailMessage

from apps.SystemService.models import Parameter


@shared_task
def send_mail(subject='', body='', from_email=None, to=None, bcc=None, attachments=None, headers=None, cc=None,
              reply_to=None):
    '''
    发送邮件
    :param subject:主题
    :param body:内容
    :param from_email:发件人
    :param to:收件人
    :param bcc:密送
    :param attachments:附件
    :param headers:
    :param cc:抄送
    :param reply_to:
    :return:
    '''
    if not from_email:
        parameter = Parameter.objects.filter(key='EMAIL', is_deleted=False).first()
        if parameter:
            value = json.loads(parameter.value)
            from_email = value.get('DEFAULT_FROM_EMAIL')

    email = EmailMessage(subject=subject, body=body, from_email=from_email, to=to, bcc=bcc, attachments=attachments,
                         headers=headers, cc=cc, reply_to=reply_to)
    email.send(fail_silently=False)
    return f'邮件已发送【主题：{subject}，发件人：{from_email}，收件人：{to}，抄送：{cc}，密送：{bcc}】'
